using System;
using System.Collections.Generic;
using System.Text;
using QLNT_DTO;
using System.Data;
using System.Data.SqlClient;

namespace QLNT_DAO
{
    public class ChiTietPhieuNhap_dao
    {
        public int ThemCTPhieuNhap(int MaThuoc, int MaDangThuoc, int MaXuatXu, int SoLuongNhap, int DonViNhap,float DonGiaNhap, DateTime ThoiGianNhap)
        {
            int n = 0;
            try
            {
                //tao list parameter {cac parameter chinh la cac tham so dau vao cua procedure}
                List<SqlParameter> listpara = new List<SqlParameter>();
                listpara.Add(new SqlParameter("@MaThuoc", MaThuoc));
                listpara.Add(new SqlParameter("@MaDangThuoc", MaDangThuoc));
                listpara.Add(new SqlParameter("@MaXuatXu", MaXuatXu));
                listpara.Add(new SqlParameter("@SoLuongNhap", SoLuongNhap));
                listpara.Add(new SqlParameter("@DonViNhap", DonViNhap));
                listpara.Add(new SqlParameter("@DonGiaNhap",DonGiaNhap));
                listpara.Add(new SqlParameter("@ThoiGianNhap", ThoiGianNhap));

                n = MyExecute.ExecuteNoneQuery("sp_ThemCTPhieuNhap", listpara);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return n;
        }

        public int HuyCTPhieuNhap(int mactphieunhap)
        {
            int n = 0;
            try
            {
                List<SqlParameter> listpara = new List<SqlParameter>();
                listpara.Add(new SqlParameter("@MaCTPhieuNhap", mactphieunhap));
                n = MyExecute.ExecuteNoneQuery("sp_XoaCTPhieuNhap", listpara);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return n;
        }

        public int ThayDoiThongTinCTPhieuNhap(int MaCTPhieuNhap, int MaThuoc, int MaDangThuoc, int MaXuatXu, int SoLuongNhap, int DonViNhap,float DonGiaNhap, DateTime ThoiGianNhap)
        {
            try
            {
                List<SqlParameter> listpara = new List<SqlParameter>();
                listpara.Add(new SqlParameter("@MaCTPhieuNhap", MaCTPhieuNhap));
                listpara.Add(new SqlParameter("@MaThuoc", MaThuoc));
                listpara.Add(new SqlParameter("@MaDangThuoc", MaDangThuoc));
                listpara.Add(new SqlParameter("@MaXuatXu", MaXuatXu));
                listpara.Add(new SqlParameter("@SoLuongNhap", SoLuongNhap));
                listpara.Add(new SqlParameter("@DonViNhap", DonViNhap));
                listpara.Add(new SqlParameter("@DonGiaNhap", DonGiaNhap));
                listpara.Add(new SqlParameter("@ThoiGianNhap", ThoiGianNhap));
                return MyExecute.ExecuteNoneQuery("sp_CapNhatCTPhieuNhap", listpara);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public ChiTietPhieuNhap_dto[] TimKiemNhanhCTPhieuNhap(string TenThuoc)
        {
            ChiTietPhieuNhap_dto[] pn = new ChiTietPhieuNhap_dto[100];
            DataTable dt = new DataTable();
            try
            {
                List<SqlParameter> listpara = new List<SqlParameter>();
                listpara.Add(new SqlParameter("@TenThuoc", TenThuoc));
                dt =  MyExecute.ExecuteQuery("sp_TimCTPhieuNhapNhanh", listpara);
                int n = dt.Rows.Count;
                pn = new ChiTietPhieuNhap_dto[n];
                for (int i = 0; i < n; i++)
                {
                    pn[i] = new ChiTietPhieuNhap_dto();
                    pn[i].MaCTPhieuNhap = int.Parse(dt.Rows[i]["MaCTPhieuNhap"].ToString());
                    pn[i].MaThuoc = int.Parse(dt.Rows[i]["MaThuoc"].ToString());
                    pn[i].MaDangThuoc = int.Parse(dt.Rows[i]["MaDangThuoc"].ToString());
                    pn[i].MaXuatXu = int.Parse(dt.Rows[i]["MaXuatXu"].ToString());
                    pn[i].SoLuongNhap = int.Parse(dt.Rows[i]["SoLuongNhap"].ToString());
                    pn[i].DonViNhap = int.Parse(dt.Rows[i]["MaDonViTinh"].ToString());
                    pn[i].DonGiaNhap = float.Parse(dt.Rows[i]["DonGiaNhap"].ToString());
                    pn[i].ThoiGianNhap = DateTime.Parse(dt.Rows[i]["ThoiGianNhap"].ToString());

                    pn[i].TenThuoc = dt.Rows[i]["TenThuoc"].ToString();
                    pn[i].TenDangThuoc = dt.Rows[i]["TenDangThuoc"].ToString();
                    pn[i].TenXuatXu = dt.Rows[i]["TenXuatXu"].ToString();
                    pn[i].SoLuongNhap = int.Parse(dt.Rows[i]["SoLuongNhap"].ToString());
                    pn[i].TenDonViNhap = dt.Rows[i]["TenDonViTinh"].ToString();
                    pn[i].DonGiaNhap = float.Parse(dt.Rows[i]["DonGiaNhap"].ToString());
                    pn[i].ThoiGianNhap = DateTime.Parse(dt.Rows[i]["ThoiGianNhap"].ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return pn;
        }

        public ChiTietPhieuNhap_dto[] SelectAll()
        {
            ChiTietPhieuNhap_dto[] pn = new ChiTietPhieuNhap_dto[100];
            DataTable dt = new DataTable();
            try
            {
                dt = MyExecute.ExecuteQuery("sp_DSCTPhieuNhap");
                int n = dt.Rows.Count;
                pn = new ChiTietPhieuNhap_dto[n];
                for (int i = 0; i < n; i++)
                {
                    pn[i] = new ChiTietPhieuNhap_dto();
                    pn[i].MaCTPhieuNhap = int.Parse(dt.Rows[i]["MaCTPhieuNhap"].ToString());
                    pn[i].MaThuoc = int.Parse(dt.Rows[i]["MaThuoc"].ToString());
                    pn[i].MaDangThuoc = int.Parse(dt.Rows[i]["MaDangThuoc"].ToString());
                    pn[i].MaXuatXu = int.Parse(dt.Rows[i]["MaXuatXu"].ToString());
                    pn[i].SoLuongNhap = int.Parse(dt.Rows[i]["SoLuongNhap"].ToString());
                    pn[i].DonViNhap = int.Parse(dt.Rows[i]["MaDonViTinh"].ToString());
                    pn[i].DonGiaNhap = float.Parse(dt.Rows[i]["DonGiaNhap"].ToString());
                    pn[i].ThoiGianNhap = DateTime.Parse(dt.Rows[i]["ThoiGianNhap"].ToString());

                    pn[i].TenThuoc = dt.Rows[i]["TenThuoc"].ToString();
                    pn[i].TenDangThuoc = dt.Rows[i]["TenDangThuoc"].ToString();
                    pn[i].TenXuatXu = dt.Rows[i]["TenXuatXu"].ToString();
                    pn[i].SoLuongNhap = int.Parse(dt.Rows[i]["SoLuongNhap"].ToString());
                    pn[i].TenDonViNhap = dt.Rows[i]["TenDonViTinh"].ToString();
                    pn[i].DonGiaNhap = float.Parse(dt.Rows[i]["DonGiaNhap"].ToString());
                    pn[i].ThoiGianNhap = DateTime.Parse(dt.Rows[i]["ThoiGianNhap"].ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return pn;
        }

        public int SelectTheoMaCTPhieuNhap(int mactphieunhap)
        {
            int ma;
            DataTable dt = new DataTable();
            try
            {
                List<SqlParameter> list = new List<SqlParameter>();
                list.Add(new SqlParameter("@MaCTPhieuNhap", mactphieunhap));
                dt = MyExecute.ExecuteQuery("sp_CTPhieuNhapTuongUng", list);
                ma = int.Parse(dt.Rows[0]["MaCTPhieuNhap"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ma;
        }

        public ChiTietPhieuNhap_dto GetCTPhieuNhap(int MaCTPhieuNhap)
        {
            List<SqlParameter> listpara = new List<SqlParameter>();
            listpara.Add(new SqlParameter("@MaCTPhieuNhap", MaCTPhieuNhap));

            DataTable dt = MyExecute.ExecuteQuery("sp_CTPhieuNhapTuongUng", listpara);
            if (dt.Rows.Count >= 1)
            {
                ChiTietPhieuNhap_dto pn = new ChiTietPhieuNhap_dto();
                pn.MaCTPhieuNhap = int.Parse(dt.Rows[0]["MaCTPhieuNhap"].ToString());
                pn.MaThuoc = int.Parse(dt.Rows[0]["MaThuoc"].ToString());
                pn.MaDangThuoc = int.Parse(dt.Rows[0]["MaDangThuoc"].ToString());
                pn.MaXuatXu = int.Parse(dt.Rows[0]["MaXuatXu"].ToString());
                pn.SoLuongNhap = int.Parse(dt.Rows[0]["SoLuongNhap"].ToString());
                pn.DonViNhap = int.Parse(dt.Rows[0]["MaDonViTinh"].ToString());
                pn.DonGiaNhap = float.Parse(dt.Rows[0]["DonGiaNhap"].ToString());
                pn.ThoiGianNhap = DateTime.Parse(dt.Rows[0]["ThoiGianNhap"].ToString());

                pn.TenThuoc = dt.Rows[0]["TenThuoc"].ToString();
                pn.TenDangThuoc = dt.Rows[0]["TenDangThuoc"].ToString();
                pn.TenXuatXu = dt.Rows[0]["TenXuatXu"].ToString();
                pn.SoLuongNhap = int.Parse(dt.Rows[0]["SoLuongNhap"].ToString());
                pn.TenDonViNhap = dt.Rows[0]["TenDonViTinh"].ToString();
                pn.DonGiaNhap = float.Parse(dt.Rows[0]["DonGiaNhap"].ToString());
                pn.ThoiGianNhap = DateTime.Parse(dt.Rows[0]["ThoiGianNhap"].ToString());
                return pn;
            }
            return null;
        }
    }
}
